import {
  getTypeList,
  editTypeList,
  getTypeName
} from '@/api/vehicle'

const state = {
  typeData: [],
  typeDataCount: 0,
  carTypeList: [],
  carNameList: []
}

const mutations = {
  GET_TYPE_DATA: (state, data) => {
    state.typeData = data
  },
  GET_TYPE_DATA_COUNT: (state, data) => {
    state.typeDataCount = data
  },
  GET_CAR_TYPE_LIST: (state, data) => {
    state.carTypeList = data
  },
  GET_CAR_NAME_LIST: (state, data) => {
    state.carNameList = data
  }
}

const actions = {
  async getTypeData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getTypeList(data)
    resp.data.data.rows.map((item) => {
      newData = [...newData, {
        id: item.id,
        name: item.name,
        state: item.state,
        ctime: item.ctime,
        type: item.type.toString(),
        sort: item.sort
      }]
    })
    // for (let i = 0; i < 5; i++) {
    //   newData = [...newData, {
    //     id: i + 1,
    //     name: '阿斯顿马丁',
    //     type: '车辆品牌',
    //     ctime: '2019-04-24 14:58:00',
    //     state: 0
    //   }]
    // }
    commit('GET_TYPE_DATA', newData)
    commit('GET_TYPE_DATA_COUNT', resp.data.data.total)
    return resp
  },
  async editTypeData({ commit }, playload) {
    const resp = await editTypeList(playload)
    return resp
  },
  async getCarTypeData({ commit }, playload) {
    const resp = await getTypeName(playload)
    commit('GET_CAR_TYPE_LIST', resp.data.data)
    return resp
  },
  async getCarNameData({ commit }, playload) {
    const resp = await getTypeName(playload)
    commit('GET_CAR_NAME_LIST', resp.data.data)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
